/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package com.auxgroup.smarthome.job;

import com.auxgroup.smarthome.entity.UserDeviceDemoEntity;
import com.auxgroup.smarthome.repo.UserDeviceDemoEntityRepository;
import com.auxgroup.smarthome.utils.common.LOG;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.dataflow.DataflowJob;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 流式处理
 * Dataflow类型
 */
@Component
public class UserDeviceDataflowJob implements DataflowJob<UserDeviceDemoEntity> {
    
    @Resource
    private UserDeviceDemoEntityRepository repository;
    
    @Override
    public List<UserDeviceDemoEntity> fetchData(final ShardingContext shardingContext) {
        List<UserDeviceDemoEntity> list = repository.getPartDataList(shardingContext.getShardingItem(),UserDeviceDemoEntity.class);
        return list.stream().collect(Collectors.toList());
    }

    @Override
    public void processData(ShardingContext shardingContext, List<UserDeviceDemoEntity> list) {
        String parameter = shardingContext.getShardingParameter();
        LOG.info(this,"分片值："+parameter+" =========== ");
        list.forEach(userDeviceEntity -> {
            if (userDeviceEntity.getTime() > System.currentTimeMillis()){
                LOG.dubug( this,"用户id:"+userDeviceEntity.getUid()+"***********did:"+userDeviceEntity.getDid() +"，发送控制指令："+ userDeviceEntity.getEvents());
            }
        });
    }

}
